
/**   
*    
* 项目名称：a_yongan   
* 类名称：OutsourcingInServiceImpl   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月30日 上午10:59:50    
* 修改时间：2018年6月30日 上午10:59:50   
* 修改备注：   
* @version    
*    
*/
package com.yongan.warehouse.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import com.yongan.user.enums.UserStatEnum;
import com.yongan.user.exception.UserException;
import com.yongan.util.Execution;
import com.yongan.warehouse.dao.GoodsinformationDao;
import com.yongan.warehouse.dao.InGoodDetailsDao;
import com.yongan.warehouse.dao.OutsourcingInDao;
import com.yongan.warehouse.entity.InGoodDetails;
import com.yongan.warehouse.entity.OutsourcingIn;
import com.yongan.warehouse.service.OutsourcingInService;

/**
 * @author liubo
 *
 */
@Service
public class OutsourcingInServiceImpl implements OutsourcingInService{

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private OutsourcingInDao outsourcingInDao;
	@Autowired
	private InGoodDetailsDao inGoodDetailsDao;
	@Autowired
	private GoodsinformationDao goodsinformationDao;
	private final String slat = "dsjdajsdkHJJHAJAKdsd921e0p12*DAe#@$@!qew";
	@Override
	public List<OutsourcingIn> getOutsourcingInList(int offset, int limit) {
		return outsourcingInDao.queryAll(offset, limit);
	}

	
	@Override
	public OutsourcingIn getOutsourcingInById(String outsourcingInId) {
		// TODO 自动生成的方法存根
		OutsourcingIn outsourcingIn = outsourcingInDao.queryById(outsourcingInId);
		outsourcingIn.setInGoodDetailsList(inGoodDetailsDao.queryAllByTopId(outsourcingInId));
		return outsourcingIn;
	}

	

	@Override
	@Transactional
	public Execution executeOutsourcingIn(OutsourcingIn outsourcingIn,
			String outsourcingInId, String md5) throws UserException {
		if(md5 == null || !md5.equals(getMD5(outsourcingInId))){
			//throw new UserException("user data rewrite");
		}
		List<InGoodDetails> inGoodDetails =null;
		try{
			if(outsourcingIn == null){
				OutsourcingIn readDell = outsourcingInDao.queryById(outsourcingInId);
				inGoodDetails = readDell.getInGoodDetailsList();
				if(!(inGoodDetails==null)){
					for(InGoodDetails goodDetails:inGoodDetails){
						reduceNumber(goodDetails, "reduce");
					}
				}
				int deleteCount = outsourcingInDao.deleteOutsourcingIn(outsourcingInId);
				int deleteInGoodDetailsCount = inGoodDetailsDao.deleteAllInGoodDetails(outsourcingInId);
				if(deleteCount<=0 & deleteInGoodDetailsCount<=0){
					throw new UserException("delete failed");
				}else{
					return new Execution(outsourcingInId, UserStatEnum.SUCCESS);
				}
			}else if((outsourcingInId.equals(""))){
				Integer hjrksl= 0 ;
				Integer hjsl= 0 ;
				Integer zje= 0 ;
				
				inGoodDetails = outsourcingIn.getInGoodDetailsList();
				int goodDetailsaddcount = 1;
				if(!(inGoodDetails==null)){
					for(InGoodDetails goodDetails:inGoodDetails){
						hjrksl = hjrksl + Integer.parseInt(goodDetails.getIngooddetailsRksl());
						hjsl = hjsl + Integer.parseInt(goodDetails.getIngooddetailsSl());
						zje = zje + Integer.parseInt(goodDetails.getIngooddetailsZje());
						goodDetails.setIngooddetailsId(UUID.randomUUID().toString());
						goodDetails.setTopId(outsourcingIn.getOutsourcinginId());
						goodDetails.setIngooddetailsType("wxrkd");
						int addInGoodDetailsCount = inGoodDetailsDao.insertInGoodDetails(goodDetails);
						if(addInGoodDetailsCount<=0){
							goodDetailsaddcount = addInGoodDetailsCount;
						}
						reduceNumber(goodDetails, "add");
					}
				}
				outsourcingIn.setOutsourcinginHjrksl(String.valueOf(hjrksl));
				outsourcingIn.setOutsourcinginHjsl(String.valueOf(hjsl));
				outsourcingIn.setOutsourcinginHjzje(String.valueOf(zje));
				int addCount = outsourcingInDao.insertOutsourcingIn(outsourcingIn);
				if(addCount<=0 & goodDetailsaddcount<=0){
					throw new UserException("add failed");
				}else{
					return new Execution(outsourcingIn.getOutsourcinginId(), UserStatEnum.SUCCESS);
				}
			}else if(!(outsourcingInId.equals(""))){
				List<InGoodDetails> oldList = inGoodDetailsDao.queryAllByTopId(outsourcingInId);
				List<String> newList = new ArrayList<String>();
				//String[] newList;
				inGoodDetails = outsourcingIn.getInGoodDetailsList();
				int goodDetailscount = 1;
				Integer hjrksl= 0 ;
				Integer hjsl= 0 ;
				Integer zje= 0 ;
				if(!(inGoodDetails==null)){
					for(InGoodDetails goodDetails:inGoodDetails){
						//加上新保存的列表
						reduceNumber(goodDetails, "add");
						hjrksl = hjrksl + Integer.parseInt(goodDetails.getIngooddetailsRksl());
						hjsl = hjsl + Integer.parseInt(goodDetails.getIngooddetailsSl());
						zje = zje + Integer.parseInt(goodDetails.getIngooddetailsZje());
						//如果更新的总单据下面的子单据ID为空，则为新增，否则是修改
						if(goodDetails.getIngooddetailsId().equals("")){
							goodDetails.setIngooddetailsId(UUID.randomUUID().toString());
							goodDetails.setTopId(outsourcingIn.getOutsourcinginId());
							goodDetails.setIngooddetailsType("wxrkd");
							int ua = inGoodDetailsDao.insertInGoodDetails(goodDetails);
							if(ua<=0){
								goodDetailscount = ua;
							}
						}else{
							//reduceNumber(goodDetails, "reduce");
							int uu = inGoodDetailsDao.updateInGoodDetails(goodDetails);
							if(uu<=0){
								goodDetailscount = uu;
							}else{
								newList.add(goodDetails.getIngooddetailsId());
							}
						}
					}
				}
				//if(!(newList.isEmpty())){
				//	System.out.println("==================================");
					if(oldList!=null){
						for(InGoodDetails old:oldList){
							//减去旧列表在库存中的数量
							reduceNumber(old, "reduce");
							Boolean sf = false;
							if(newList!=null){
								for(String newl:newList){
									if(old.getIngooddetailsId().equals(newl)){
										sf = true;
									}
								}
								if(!sf){
									inGoodDetailsDao.deleteInGoodDetails(old.getIngooddetailsId());
								}
							//}else{
							//	inGoodDetailsDao.deleteAllInGoodDetails(outsourcingInId);
							}
						}
					}
				//}else{
					//inGoodDetailsDao.deleteAllInGoodDetails(outsourcingInId);
				//}
				
				outsourcingIn.setOutsourcinginHjrksl(String.valueOf(hjrksl));
				outsourcingIn.setOutsourcinginHjsl(String.valueOf(hjsl));
				outsourcingIn.setOutsourcinginHjzje(String.valueOf(zje));
				int updateCount = outsourcingInDao.updateOutsourcingIn(outsourcingIn);
				if(updateCount<=0 & goodDetailscount<=0){
					throw new UserException("update failed");
				}else{
					return new Execution(outsourcingInId, UserStatEnum.SUCCESS);
				}
			}else{
				throw new UserException("parameter error");
			}
			
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			throw new UserException("inner error:"+e.getMessage());
		}
	}

	
	@Override
	public int getCount() {
		// TODO 自动生成的方法存根
		return outsourcingInDao.queryCount();
	}
	
	private String getMD5(String Id){
		String base = Id + "/" + slat;
		String md5 = DigestUtils.md5DigestAsHex(base.getBytes());
		return md5;
	}
	
	public OutsourcingInServiceImpl() {
		super();
		// TODO 自动生成的构造函数存根
	}


	public int reduceNumber(InGoodDetails igd,  String type){
		return goodsinformationDao.reduceNumber(igd.getIngooddetailsNumber()
				, type, Integer.parseInt(igd.getIngooddetailsSl())
				, Integer.parseInt(igd.getIngooddetailsRksl()));
	}
	
}
